/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 * Since 2008 - 2012
 */

package com.smg.community.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javacommon.base.BaseIbatis3Dao;

import org.springframework.stereotype.Repository;

import com.smg.community.model.CrmRight;
import com.smg.community.vo.query.CrmRightQuery;

import cn.org.rapid_framework.page.Page;



@SuppressWarnings("unchecked")
@Repository
public class CrmRightDao extends BaseIbatis3Dao<CrmRight,java.lang.Integer>{
	
	@Override
	public String getIbatisMapperNamesapce() {
		return "CrmRight";
	}
	
	public void saveOrUpdate(CrmRight entity) {
		if(entity.getId() == null) 
			save(entity);
		else 
			update(entity);
	}
	
	/**
	 * 根据角色id获取对应的权限
	 * */
	public List<CrmRight> getAllRightByRoleId(String roleIds,String parentCode){
		Map<String, String> param = new HashMap<String, String>();
		param.put("roleIds", roleIds);
		param.put("parentCode", parentCode);
		return getSqlSessionTemplate().selectList("CrmRight.getAllRightByRoleId", param);
	}
	public CrmRight getBy(CrmRightQuery query){
		return (CrmRight) getSqlSessionTemplate().selectOne("CrmRight.getBy", query);
	}
	
	/**
	 * 通过名称查询权限
	 * @param text
	 * @return
	 */
	public CrmRight getByText(String text){
		Map<String, String> param = new HashMap<String, String>();
		param.put("text", text.trim());
		return (CrmRight) getSqlSessionTemplate().selectOne("CrmRight.getByText", param);
	}
	
	public List<CrmRight> getAll(){
		return getSqlSessionTemplate().selectList("CrmRight.getAll", null);
	}
	
	/**
	 * 分页查询数据
	 * @param query
	 * @return
	 */
	public Page<CrmRight> findPage(CrmRightQuery query) {
		return pageQuery("CrmRight.findPage",query);
	}
	
	/**
	 * 获取二级菜单最大的排序位置
	 * @return
	 */
	public int getMaxIndex (String parentCode) {
		Map<String, String> param = new HashMap<String, String>();
		param.put("parentCode", parentCode);
		return Integer.parseInt(super.getSqlSessionTemplate().selectOne("CrmRight.findMaxSort", param) + "");
	}
	
	/**
	 * 移动权限的顺序
	 * @param smallIndex 小位置
	 * @param bigIndex 大位置
	 * @param index 是+1还是-1
	 * @param parentCode 父级code
	 */
	public int move(int smallIndex, int bigIndex, int index, String parentCode) {
		Map<String, String> param = new HashMap<String, String>();
		param.put("smallIndex", smallIndex + "");
		param.put("bigIndex", bigIndex + "");
		param.put("index", index + "");
		param.put("parentCode", parentCode);
		return super.getSqlSessionTemplate().update("CrmRight.updateRightSort", param);
	}
	
	/**
	 * 查询子级菜单权限
	 * @param parentCode
	 * @return
	 */
	public List<CrmRight> findChidrenPers(String parentCode) {
		Map<String, String> parameter = new HashMap<String, String>();
		parameter.put("parentCode", parentCode.trim());
		return super.getSqlSessionTemplate().selectList("CrmRight.getBy", parameter);
	}
	
	/**
	 * 根据父级code获取子级权限最大的code和rightSort
	 * @param parentCode
	 * @return
	 */
	public CrmRight getMaxCodeAndSort(String parentCode) {
		Map<String, String> param = new HashMap<String, String>();
		param.put("parentCode", parentCode);
		return (CrmRight) super.getSqlSessionTemplate().selectOne("CrmRight.getMaxCodeAndSort", param);
	}
}
